﻿using EmprestaAE.Common;
using EmprestaAE.MVVM.Model;
using EmprestaAE.MVVM.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace EmprestaAE.MVVM.ViewModel
{
    public class MainVM : ViewModelBase
    {
        #region Singleton
        private static MainVM instance;

        public static MainVM Instance
        {
            get { return instance ?? (instance = new MainVM()); }            
        }
        
        #endregion

        private ObservableCollection<Lending> lends;

        public ObservableCollection<Lending> Lends
        {
            get { return lends ?? (lends = new ObservableCollection<Lending>()); }
            set { if (lends != value) { lends = value; RaisePropertyChanged("Lends"); } }
        }

        private ObservableCollection<Borrowing> borrowings;

        public ObservableCollection<Borrowing> Borrowings
        {
            get { return borrowings ?? (borrowings = new ObservableCollection<Borrowing>()); }
            set { if (borrowings != value) { borrowings = value; RaisePropertyChanged("Borrowings"); } }
        }

        public MainVM()
        {
            Lends.CollectionChanged += Lends_CollectionChanged;
            PropertyChanged += MainVM_PropertyChanged;
            Window.Current.Activated += Current_Activated; 
        }

        void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
            {   

            }

        }

        private VMCommand addLendingCommand;

        public VMCommand AddLendingCommand
        {
            get { return addLendingCommand ?? (addLendingCommand = new VMCommand(AddLendingCommandAction)); }
            set { addLendingCommand = value; }
        }

        private async void AddLendingCommandAction(object obj)
        {
            Lends.Add(obj as Lending);
            LocalStorageHelper.Data.Add(obj);
            LocalStorageHelper.Save<Lending>();

            LocalStorageHelper.Restore<Lending>();
            List<object> data = LocalStorageHelper.Data;
            ObservableCollection<Lending> lends = new ObservableCollection<Lending>();
            foreach (Lending lending in data)
            {
                lends.Add(lending);
            }

            MainVM.Instance.Lends = lends;
        }


        private VMCommand addBorrowingCommand;

        public VMCommand AddBorrowingCommand
        {
            get { return addBorrowingCommand ?? (addBorrowingCommand = new VMCommand(AddBorrowingCommandAction)); }
            set { addBorrowingCommand = value; }
        }

        private async void AddBorrowingCommandAction(object obj)
        {
            Borrowings.Add(obj as Borrowing);
            LocalStoragBorrowingsHelper.Data.Add(obj);
            LocalStoragBorrowingsHelper.Save<Borrowing>();

            LocalStorageHelper.Restore<Lending>();
            LocalStoragBorrowingsHelper.Restore<Borrowing>();
            List<object> data = LocalStoragBorrowingsHelper.Data;
            ObservableCollection<Borrowing> borrows = new ObservableCollection<Borrowing>();
            foreach (Borrowing borrowing in data)
            {
                borrows.Add(borrowing);
            }

            MainVM.Instance.Borrowings = borrows;
        }

        private VMCommand itemViewCommand;

        public VMCommand ItemViewCommand
        {
            get { return itemViewCommand ?? (itemViewCommand = new VMCommand(ItemViewCommandAction)); }            
        }

        private Lending lendingViewLending;

        public Lending LendingViewLending
        {
            get { return lendingViewLending; }
            set { lendingViewLending = value; }
        }

        private Borrowing borrowingViewBorrowing;

        public Borrowing BorrowingViewBorrowing
        {
            get { return borrowingViewBorrowing; }
            set { borrowingViewBorrowing = value; }
        }
        

        private void ItemViewCommandAction(object obj)
        {
            if (obj is Lending)
            {
                LendingViewLending = obj as Lending;
                NavigationService.NavigateCommand.Execute(new LendingView());
            }
            else
                if (obj is Borrowing)
                {
                    BorrowingViewBorrowing = obj as Borrowing;
                    NavigationService.NavigateCommand.Execute(new BorrowingView());
                }
            
        }

        private VMCommand returnCommand;

        public VMCommand ReturnCommand
        {
            get { return returnCommand ?? (returnCommand = new VMCommand(ReturnCommandAction)); }
            set { returnCommand = value; }
        }

        private void ReturnCommandAction(object obj)
        {
            if (obj is Lending)
            {
                Lending returningLending = obj as Lending;
                bool result = MainVM.Instance.Lends.Remove(returningLending);
                LocalStorageHelper.Data.Remove(obj);
                LocalStorageHelper.Save<Lending>();

                LocalStorageHelper.Restore<Lending>();
                List<object> data = LocalStorageHelper.Data;
                ObservableCollection<Lending> lends = new ObservableCollection<Lending>();
                foreach (Lending lending in data)
                {
                    lends.Add(lending);
                }

                MainVM.Instance.Lends = lends;
            }

            if (obj is Borrowing)
            {
                Borrowing returningBorrowing = obj as Borrowing;
                MainVM.Instance.Borrowings.Remove(returningBorrowing);
                LocalStoragBorrowingsHelper.Data.Remove(obj);
                LocalStoragBorrowingsHelper.Save<Borrowing>();

                LocalStorageHelper.Restore<Lending>();
                LocalStoragBorrowingsHelper.Restore<Borrowing>();
                List<object> data = LocalStoragBorrowingsHelper.Data;
                ObservableCollection<Borrowing> borrows = new ObservableCollection<Borrowing>();
                foreach (Borrowing borrowing in data)
                {
                    borrows.Add(borrowing);
                }

                MainVM.Instance.Borrowings = borrows;
            }

            NavigationService.GoBackCommand.Execute(obj);
            
        }

        void MainVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Lends":
                    break;
                default:
                    break;
            }
        }

        void Lends_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
        }
        

        #region Properties
        
        #endregion


        #region Commands

        

        #endregion
    }
}
